package com.iter.devbox.juc;

import java.util.concurrent.Exchanger;

public class MyExchanger {

	public static void main(String[] args) {
		Exchanger<String> exchanger = new Exchanger<String>();  
		  
		ExchangerRunnable exchangerRunnable1 = new ExchangerRunnable(exchanger, "A");  
		ExchangerRunnable exchangerRunnable2 = new ExchangerRunnable(exchanger, "B");  
		  
		new Thread(exchangerRunnable1).start();  
		new Thread(exchangerRunnable2).start();  
	}
}

class ExchangerRunnable implements Runnable {
	
	Exchanger<String> exchanger = null;
    String object    = null;
  
    public ExchangerRunnable(Exchanger<String> exchanger, String object) {
        this.exchanger = exchanger;
        this.object = object;
    }
    
	@Override
	public void run() {
		try {
            Object previous = this.object;
            //两个线程通过exchange方法交换数据
            this.object = this.exchanger.exchange(this.object);
            System.out.println(Thread.currentThread().getName() + " exchanged " + previous + " for " + this.object);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }  
	}
}
